package com.battery.web.controller.system.battery;

import java.util.List;

import com.battery.common.utils.DateUtils;
import com.battery.system.service.battery.IAdvertService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Advert;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 广告Controller
 *
 * @author battery
 * @date 2021-08-03
 */
@Controller
@RequestMapping("/system/advert")
public class AdvertController extends BaseController {
    private String prefix = "system/advert";

    @Autowired
    private IAdvertService advertService;

    @RequiresPermissions("system:advert:view")
    @GetMapping()
    public String advert() {
        return prefix + "/advert";
    }

    /**
     * 查询广告列表
     */
    @RequiresPermissions("system:advert:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Advert advert) {
        startPage();
        List<Advert> list = advertService.selectAdvertList(advert);
        return getDataTable(list);
    }

    /**
     * 导出广告列表
     */
    @RequiresPermissions("system:advert:export")
    @Log(title = "广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Advert advert) {
        List<Advert> list = advertService.selectAdvertList(advert);
        ExcelUtil<Advert> util = new ExcelUtil<Advert>(Advert.class);
        return util.exportExcel(list, "advert");
    }

    /**
     * 新增广告
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存广告
     */
    @RequiresPermissions("system:advert:add")
    @Log(title = "广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Advert advert) {
        if (advert.getUseSceneType() == null) {
            return error("请选择使用场景类型");
        }
        if (advert.getIsClose() == null) {
            return error("请选择是否关闭");
        }
        if (StringUtils.isBlank(advert.getImg())) {
            return error("请上传图片");
        }
        boolean isCz = advertService.checkUseScene(advert.getUseSceneType());
        if (isCz) {
            return error("该场地已有广告,请选择其他类型");
        }
        advert.setCreateTime(DateUtils.getNowDate());
        return toAjax(advertService.insertSelective(advert));
    }

    /**
     * 修改广告
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Advert advert = advertService.selectAdvertById(id);
        mmap.put("advert", advert);
        return prefix + "/edit";
    }

    /**
     * 修改保存广告
     */
    @RequiresPermissions("system:advert:edit")
    @Log(title = "广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Advert advert) {
        if (advert.getId() == null) {
            return error("ID为空");
        }
        if (advert.getIsClose() == null) {
            return error("请选择是否关闭");
        }
        if (StringUtils.isBlank(advert.getImg())) {
            return error("请上传图片");
        }
        advert.setUpdateTime(DateUtils.getNowDate());
        return toAjax(advertService.updateByPrimaryKeySelective(advert));
    }

    /**
     * 删除广告
     */
    @RequiresPermissions("system:advert:remove")
    @Log(title = "广告", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(advertService.deleteAdvertByIds(ids));
    }
}
